iT邦幫忙

2022 iThome 鐵人賽

DAY 18
0
Modern Web

clojure 刷刷鍋系列 第 18

Clojure 肉片 -第 18 塊

  • 分享至 

  • xImage
  •  

【今日湯底】

Complete the solution so that it reverses all of the words within the string passed in.

Example(Input --> Output):

"The greatest victory is that which requires no battle" --> "battle no requires which that is victory greatest The"

(必須通過以下測試)

(ns kata.test
  (:require [clojure.test :refer :all]
            [kata         :refer [reverse-words]]))

(deftest basic-tests
  (is (= (reverse-words "hello world!")                , "world! hello"))
  (is (= (reverse-words "yoda doesn't speak like this"), "this like speak doesn't yoda"))
  (is (= (reverse-words "foobar")                      , "foobar"))
  (is (= (reverse-words "kata editor")                 , "editor kata"))
  (is (= (reverse-words "row row row your boat")       , "boat your row row row")))

【我的答案】

(ns kata)
(defn reverse-words [words]
  (->> (clojure.string/split words #" ")
    reverse
    (clojure.string/join #" "))
  )
(ns kata)
(defn reverse-words [words]
  (->> (clojure.string/split words #" ")
    reverse
    (reduce #(str %1 " " %2) " ")
    (clojure.string/trim))
  )

【其他人的答案】

(ns kata
  (require [clojure.string :as str]))

(defn reverse-words [words]
  (->> (str/split words #" ")
       (reverse)
       (str/join " ")))

上一篇
Clojure 肉片 -第 17 塊
下一篇
Clojure 肉片 -第 19 塊
系列文
clojure 刷刷鍋30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言